package mytest;

import com.hazelcast.config.Config;
import com.hazelcast.config.GroupConfig;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.dns.AddressResolverOptions;
import io.vertx.core.http.HttpServer;
import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.ext.web.Router;
import io.vertx.servicediscovery.Record;
import io.vertx.servicediscovery.ServiceDiscovery;
import io.vertx.servicediscovery.ServiceDiscoveryOptions;
import io.vertx.servicediscovery.types.HttpEndpoint;
import io.vertx.spi.cluster.hazelcast.HazelcastClusterManager;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Ldj
 * @Date 2017/11/2 9:30
 * @Description 数据处理
 */
public class jqd extends AbstractVerticle {

    // Maintain a simple list of names维持名称简单列表
    private List names = new ArrayList<>();
    @Override
    public void start(){
        ServiceDiscovery discovery = ServiceDiscovery.create(vertx,
                new ServiceDiscoveryOptions()
                        .setAnnounceAddress("service-announce")
                        .setName("my-name"))
                ;

        Router router = Router.router(vertx);
        // Register a simple first route on / 注册一个简单首个route在根路径/
        router.get("/").handler(rc -> {
            rc.response().end("Welcome");
        });
        // Register a second router retrieving all stored names as JSON注册第二个router返回名称列表
        router.get("/names").handler(
                // Just encode the list as JSON and return.
                rc -> rc.response()
                        .putHeader("content-type", "application/json")
                        .end("服务二"));
        Future<HttpServer> httpServerFuture = Future.future();
        vertx.createHttpServer()
                .requestHandler(router::accept)
                .listen(8082, "localhost", httpServerFuture.completer());

       /* // create a record from type
        Record record2 = HttpEndpoint.createRecord("some-rest-api", "localhost", 8082, "/");
        //publish the service
        discovery.publish(record2, ar -> {
            if (ar.succeeded()) {
                System.out.println("\"" + record2.getName() + "\" successfully published!---222");
                Record publishedRecord = ar.result();
            } else {
                // publication failed
            }
        });*/
    }

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
       /* Config cfg = new Config();
        // 加入组的配置，防止广播环境下，负载串到别的开发机中
        GroupConfig group = new GroupConfig();
        group.setName("p-dev");
        group.setPassword("p-dev");
        cfg.setGroupConfig(group);
        // 申明集群管理器
        ClusterManager mgr = new HazelcastClusterManager(cfg);
        VertxOptions options = new VertxOptions();
        options.
                setWorkerPoolSize(40)
                .setClustered(true)
                .setHAEnabled(true)
                .setClusterHost("localhost")
                .setClusterPort(8001)
                .setClusterManager(mgr)
                .setHAGroup("test-service")
                .setAddressResolverOptions(new AddressResolverOptions()
                        .addServer("127.0.0.1:7001"));
        // 集群化vertx
        Vertx.clusteredVertx(options, ar -> {
            if (ar.succeeded()) {
                System.out.println(" successfully clusteredVertx!");
                 ar.result();
            } else {
                System.out.println("cluster failed, using default vertx");
            }
        });*/
        /*VertxOptions options = new VertxOptions();
        options.
                setWorkerPoolSize(40)
               *//* .setHAEnabled(true)
                .setHAGroup("test-service")*//*
                .setAddressResolverOptions( new AddressResolverOptions().
                        addServer("192.168.1.51:8001"));*/
        // 集群化vertx
        //Vertx.vertx(options);
        vertx.deployVerticle(jqd.class.getName());
        //vertx.deployVerticle(jqc.class.getName());
    }
}
